package com.jeremyfeinstein.conference;

import android.widget.LinearLayout;

import com.intel.webrtc.base.LocalStream;
import com.intel.webrtc.base.Stream.VideoRendererInterface;
import com.intel.webrtc.base.WoogeenIllegalArgumentException;
import com.intel.webrtc.base.WoogeenSurfaceRenderer;
import com.jeremyfeinstein.utils.WoogeenSampleView;

import org.webrtc.RendererCommon;

public class LocalStreamView {
	private LinearLayout mContainer;
	private WoogeenSampleView sampleView;
	private WoogeenSurfaceRenderer surfaceRenderer;
	private VideoRendererInterface streamRenderer;
	private LocalStream mLocalStream;

	public LocalStreamView(LinearLayout container) {
		setContainer(container);
		setSampleView(new WoogeenSampleView(container.getContext()));
		setSurfaceRenderer(new WoogeenSurfaceRenderer(getSampleView()));
		getContainer().addView(getSampleView());
		setStreamRenderer(getSurfaceRenderer().createVideoRenderer(0, 0, 100,
				100, RendererCommon.ScalingType.SCALE_ASPECT_FILL, false));
	}


	public void playLocalStream(final LocalStream localStream) {
		if (this.getLocalStream() != localStream) {
			if (this.getLocalStream() != null) {
				mContainer.post(new Runnable() {
					@Override
					public void run() {
						try {
							getLocalStream().detach(streamRenderer);
						} catch (WoogeenIllegalArgumentException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
			}
			mContainer.post(new Runnable() {
				@Override
				public void run() {
					try {
						localStream.attach(streamRenderer);
					} catch (WoogeenIllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}
		this.setLocalStream(localStream);
	}

	public void stop() {
		if (this.getLocalStream() != null) {
			mContainer.post(new Runnable() {
				@Override
				public void run() {
					try {
						getLocalStream().detach(streamRenderer);
					} catch (WoogeenIllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		}
		getStreamRenderer().cleanFrame();
	}

	public void pause() {
		if (this.getLocalStream() != null) {
			this.getLocalStream().disableAudio();
			this.getLocalStream().disableVideo();
		}
	}

	public void resume() {
		if (this.getLocalStream() != null) {
			this.getLocalStream().enableAudio();
			this.getLocalStream().enableVideo();
		}
	}

	public LinearLayout getContainer() {
		return mContainer;
	}

	public void setContainer(LinearLayout container) {
		this.mContainer = container;
	}

	public WoogeenSampleView getSampleView() {
		return sampleView;
	}

	public void setSampleView(WoogeenSampleView sampleView) {
		this.sampleView = sampleView;
	}

	public WoogeenSurfaceRenderer getSurfaceRenderer() {
		return surfaceRenderer;
	}

	public void setSurfaceRenderer(WoogeenSurfaceRenderer surfaceRenderer) {
		this.surfaceRenderer = surfaceRenderer;
	}

	public VideoRendererInterface getStreamRenderer() {
		return streamRenderer;
	}

	public void setStreamRenderer(VideoRendererInterface streamRenderer) {
		this.streamRenderer = streamRenderer;
	}

	public LocalStream getLocalStream() {
		return mLocalStream;
	}

	public void setLocalStream(LocalStream localStream) {
		this.mLocalStream = localStream;
	}
}
